<?PHP 
function get_sql($table,$key,$where,$options,$innerjoin)	
{
	
	global $conf;
	$v=TRUE;
	
	$order=$options['order'];
	$limit=$options['limit'];
	$count=$options['count'];
	$string=$options['string'];
	
	//$innerjoin[0] yes/no
	//$innerjoin[1] table where to get shit from
	//$innerjoin[2][0] items to get
	//$innerjoin[3][0] stuff to match

	if(empty($where))
	
	{
		
		if($count)
		{
			$sql="SELECT count(*) FROM `".$table."` ";	
		}
		else if($innerjoin[0])
		{
			
			
			foreach($innerjoin[2] as $x)
			{
				$ijitems.=$innerjoin[1].".".$x.",";
			}
			$sql="SELECT ".$table.".*, ".$ijitems." FROM `".$table."` ";	
			
			$sql.="INNER JOIN `".$innerjoin[1]."` ON ".$table.".".$innerjoin[3][0]." = ".$innerjoin[1].".".$innerjoin[3][1]." ";
		}
		else
		{
			$sql="SELECT * FROM `".$table."` ";	
		}
			
		
	}
	else
	{
		
			if($count)
			{
				$sql="SELECT count(*) FROM `".$table."` WHERE ";	
			}
			else if($innerjoin[0])
			{

				$ijlimit=count($innerjoin[2]);
				$ijcount=1;
				foreach($innerjoin[2] as $x)
				{
					if($ijlimit<=$ijcount)
					{
						$ijitems.=$innerjoin[1].".".$x."".$ijs." ";
					}
					else
					{
							$ijitems.=$innerjoin[1].".".$x."".$ijs.", ";
					}
					$ijcount++;
				}
				
				
				$sql="SELECT ".$table.".*, ".$ijitems." FROM `".$table."` ";	

				$sql.="INNER JOIN `".$innerjoin[1]."` ON ".$table.".".$innerjoin[3][0]." = ".$innerjoin[1].".".$innerjoin[3][1]." WHERE ";
			}
			else
			{
				$sql="SELECT * FROM `".$table."` WHERE ";	
			}
		
		foreach ($where as $x)
		{
			if($v)
			{
				if($string)
					{
								$sql.=" ".$table.".".$x[0]." ".$x[2]." CONVERT( _utf8 '%".$x[1]."%'
USING latin1 ) 
	COLLATE latin1_swedish_ci ";
					}
				else
					{
								$sql.=" ".$table.".".$x[0]." ".$x[2]." '".$x[1]."'";
					}
		
				$v=FALSE;
			}
			else
			
			{
				
					if($string)
					{
								$sql.=" AND ".$table.".".$x[0]." ".$x[2]." CONVERT( _utf8 '%".$x[1]."%'
USING latin1 ) 
	COLLATE latin1_swedish_ci ";
					}
					else
					{
								$sql.=" AND ".$table.".".$x[0]." ".$x[2]." '".$x[1]."'";
					}
					
			}
	

			
		}
	
		

	}
//	////rp($order);
	if(!empty($order))
	{
		$sql.=" ORDER BY `".$order[0]."` ".$order[1]." ";
	
	}
	if(!empty($limit))
	{
		$sql.=" LIMIT ".$limit[0].", ".$limit[1]." ";
	}
	
	global $conf;


	
			$message= "<div class='sql_statement'>sql: ".$sql."</div>";
			//mp($message);
			
	if($conf->debug)
	{
			$conf->queries++;
		error_log($sql." queries: ".$conf->queries,0);
		//echo "<div class='sm'>".$message."</div>";
	
	}
		
	//	
	
//	error_log($sql,0);
								$result = mysql_query($sql); 
				
							
						
							
								while($row=mysql_fetch_array($result))
								{	
									$akey=$row[$key];
									$arr[$akey]=$row;

								
								}
							//	////rp($arr);
						
						
								return $arr;
}
function get_select_values($table,$key)
{
	
//	get_sql($table,$fields,$key,$where,$order,$limit,$count)	
	
	$data=get_sql($table,$key,NULL,NULL);
//	$data = get_sql($table,NULL,NULL,NULL,NULL,FALSE);
	
	return $data;
}
function get_data_form($table,$key)
{
	$appt[0][0]=$key;
	$appt[0][1]=$_GET[$key];
	$appt[0][2]='=';	
	
		
	$data = getlistfiltered2($table,NULL,NULL,$appt,NULL,NULL,FALSE);
	
	return $data[0];
}
function sql_insert($v)
{		

		$rowc=count($v['rows'])-1;
		$valc=count($v['values'])-1;
		if($v['replace'])
		{
			$sql="REPLACE into `".$v['table']."` (";
		}
		else
		{
			$sql="INSERT into `".$v['table']."` (";
		}
		
		
		
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."`";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.") VALUES (";
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." '". cleanforsql($values[$i])."'";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.")";
		//echo $sql;
		mysql_query($sql) or die (mysql_error());
}
function sql_update($v)
{
	
}

function sql_get_list($v)
{
	global $conf;
	$y=TRUE;
	
	if(empty($v['where']))
	{
		if($v['count'])
		{
			$sql="SELECT count(*) FROM `".$v['table']."` ";	
		}
		else
		{
			$sql="SELECT * FROM `".$v['table']."` ";	
		}
			
		
	}
	else
	{
		
			if($count)
			{
				$sql="SELECT count(*) FROM `".$v['table']."` WHERE ";	
			}
			else
			{
				$sql="SELECT * FROM `".$v['table']."` WHERE ";	
			}
		
		foreach ($v['where'] as $x)
		{
			if($y)
			{
				$sql.=" `".$x[0]."` ".$x[2]." '".$x[1]."'";
				$y=FALSE;
			}
			else
			
			{
					$sql.=" AND `".$x[0]."` ".$x[2]." '".$x[1]."'";
			}
	

			
		}
	
		

	}
//	////rp($order);
	if(!empty($v['order']))
	{
		$sql.=" ORDER BY `".$v['order'][0]."` ".$v['order'][1]." ";
	
	}
	if(!empty($v['limit']))
	{
		$sql.=" LIMIT ".$v['limit'][0].", ".$v['limit'][1]." ";
	}
		

								$result = mysql_query($sql); 
				
							
						
							if(!empty($result))
							{
								while($row=mysql_fetch_array($result))
								{	
						
									$arr[]=$row;

								
								}
								
								//	if($conf->debug){echo "sql: ".$sql."<br />";};
							//	////rp($arr);
						
							
								return $arr;
							}
							else
							{
								return false;
							}
							
}

function process_serialized_values($post)
{
	
			//function checks serialized variables and serializes them
			foreach($post as $post_key => $value)
			{

				$haystack = $post_key;
				$needle = '_';
				$result = substr($haystack, 0, strpos($haystack, $needle));	

				if($result=='serial')
				{
				$y[$post_key]=$post[$post_key];	
				}
			}
			$post['content_serial']=serialize($y);
			return $post;
	
}

function process_serialized_data($post,$key,$variable)
{
	
			//function checks serialized variables and serializes them
			foreach($post as $post_key => $value)
			{

				$haystack = $post_key;
				$needle = '_';
				$result = substr($haystack, 0, strpos($haystack, $needle));	

				if($result==$key)
				{
				$y[$post_key]=$post[$post_key];	
				}
			}
			$post[$variable]=serialize($y);
			return $post;
	
}

function process_default_serialized_values($post)
{
	
			//function checks serialized variables and serializes them
			foreach($post as $post_key => $value)
			{

				$haystack = $post_key;
				$needle = '_';
				$result = substr($haystack, 0, strpos($haystack, $needle));	

				if($result=='serial')
				{
				$y[$post_key]=$post[$post_key];	
				}
			}
			$post['serial']=serialize($y);
			return $post;
	
}
	
function cleanforsql($str)
{
	
	$str=str_replace("'","&#039;",$str);
	return $str;
}


// mod_SQL.php
	function randomchar($num,$lower,$date)
		{
			if(empty($num))
			{
				$n=6;
			}
			else
			{
				$n=$num;
			}
		
		$str = '';
					for ($i=1; $i<=$n; $i++){
					$set = array(rand (65,90),rand(97,122));
					$str .= chr($set[rand(0,1)]);
					}
		if($lower)
		{
			$str=strtolower($str);
		}
		
		if($date)
		{
			$str=date("Ymd")."_".$str;
		}
		return $str;		
		}
		
		
		
		
function getlist($table,$field,$key,$keyvalue)	
{
	
	if(empty($key))
	{
	$sql="SELECT * FROM `".$table."` ";		
	}
	else
	{
	$sql="SELECT * FROM `".$table."` WHERE `".$key."` =".$keyvalue." ";		
	}


								$result = mysql_query($sql); 
				
							
						
							
								while($row=mysql_fetch_array($result))
								{	
						
									$arr[]=$row;

								
								}
							
								return $arr;
}	


function getlistfiltered($table,$fields,$key,$where)	
{
	$v=TRUE;
	if(empty($where))
	{
	$sql="SELECT * FROM `".$table."` ";		
	}
	else
	{
		
		$sql="SELECT * FROM `".$table."` WHERE ";	
	//	////rp($where);
		foreach ($where as $x)
		{
			if($v)
			{
				$sql.=" `".$x[0]."` = '".$x[1]."'";
				$v=FALSE;
			}
			else
			
			{
					$sql.=" AND `".$x[0]."` = '".$x[1]."'";
			}
			
		//	echo "ebak";
			
		}
	
		

	}
	//echo $sql."<br/>";


								$result = mysql_query($sql); 
				
							
						
							
								while($row=mysql_fetch_array($result))
								{	
						
									$arr[]=$row;

								
								}
							//	////rp($arr);
							
								return $arr;
}



function getlistfiltered2($table,$fields,$key,$where,$order,$limit,$count)	
{
	$v=TRUE;
	
	if(empty($where))
	{
		if($count)
		{
			$sql="SELECT count(*) FROM `".$table."` ";	
		}
		else
		{
			$sql="SELECT * FROM `".$table."` ";	
		}
			
		
	}
	else
	{
		
			if($count)
			{
				$sql="SELECT count(*) FROM `".$table."` WHERE ";	
			}
			else
			{
				$sql="SELECT * FROM `".$table."` WHERE ";	
			}
		
		foreach ($where as $x)
		{
			if($v)
			{
				$sql.=" `".$x[0]."` ".$x[2]." '".$x[1]."'";
				$v=FALSE;
			}
			else
			
			{
					$sql.=" AND `".$x[0]."` ".$x[2]." '".$x[1]."'";
			}
	

			
		}
	
		

	}
//	////rp($order);
	if(!empty($order))
	{
		$sql.=" ORDER BY `".$order[0]."` ".$order[1]." ";
	
	}
	if(!empty($limit))
	{
		$sql.=" LIMIT ".$limit[0].", ".$limit[1]." ";
	}
		
	//	echo "sql: ".$sql."<br />";
								$result = mysql_query($sql); 
				
							
						
							
								while($row=mysql_fetch_array($result))
								{	
						
									$arr[]=$row;

								
								}
							//	////rp($arr);
						
							
								return $arr;
}
function getval($table,$field,$key,$keyvalue)
{
	$sql="SELECT * FROM `".$table."` WHERE `".$key."` =".$keyvalue." ";
//	echo $sql;
								$result = mysql_query($sql); 
								while($row=mysql_fetch_array($result))
								{	
								$val=$row[$field];
								
								}
								
								return $val;

}

function getval3($table,$field,$key,$keyvalue)
{
	$sql ="
		 SELECT *
		FROM `".$table."`
		WHERE `".$key."` LIKE CONVERT( _utf8 '".$keyvalue."'
		USING latin1 )
		COLLATE latin1_swedish_ci
	
	";
//	$sql="SELECT * FROM `".$table."` WHERE `".$key."` LIKE CONVERT( _utf8 `".$keyvalue."` USING latin1 ) COLLATE latin1_swedish_ci ";
//	echo $sql;
								$result = mysql_query($sql); 
								while($row=mysql_fetch_array($result))
								{	
								$val=$row[$field];
								
								}
								
								return $val;

}

function getval2($table,$field,$key,$keyvalue)
{
	$sql="SELECT * FROM `".$table."` ";
	
								$result = mysql_query($sql); 
							$row=mysql_fetch_array($result);
								
								
								return $row;

}

function deletesql($table,$field,$key)
{
	$sql="DELETE FROM `".$table."` WHERE `".$table."`.`".$field."` = ".$key." LIMIT 1";
	$result = mysql_query($sql); 
	
}

function delete_sql($table,$field,$key,$string)
{
	if($string)
	{
		$key="'".$key."'";
	}
	$sql="DELETE FROM `".$table."` WHERE `".$table."`.`".$field."` = ".$key." LIMIT 1";
	$result = mysql_query($sql); 
	
	
	global $conf;


	
			$message= "<div class='sql_statement'>sql: ".$sql."</div>";
			//mp($message);
			
	if($conf->debug)
	{
		
		echo "<div class='sm'>".$message."</div>";
	
	}
	//echo $sql;
	
}

function delete_sql_dual($table,$field,$key,$string,$field2,$key2)
{
	if($string)
	{
		$key="'".$key."'";
		$key2="'".$key2."'";
	}
	$sql="DELETE FROM `".$table."` WHERE `".$table."`.`".$field."` = ".$key." AND `".$table."`.`".$field2."` = ".$key2."  LIMIT 1";
	$result = mysql_query($sql); 
	
	//echo $sql;
	
}
function getday()
{	

		$dt=time();
		$dc=mktime(0, 0, 0, date("m",$dt), date("d",$dt),date("Y",$dt));
		return $dc;
}

function insertsql($table,$rows,$values)
{		

		$rowc=count($rows);
		$valc=count($values);
		
		$sql="INSERT into `".$table."` (";
		
		
					for($i=1; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."`";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.") VALUES (";
					for($i=1; $i<=$rowc; $i++)
					{
					
						$sql=$sql." '". cleanforsql($values[$i])."'";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.")";
	//	echo $sql;
		mysql_query($sql) or die (mysql_error());
}


function insertsqlv2($table,$rows,$values)
{		

		$rowc=count($rows)-1;
		$valc=count($values)-1;
		
		$sql="INSERT into `".$table."` (";
		
		
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."`";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.") VALUES (";
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." '". cleanforsql($values[$i])."'";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.")";
		//echo $sql;
		mysql_query($sql) or die (mysql_error());
}

function insert_sql($table,$rows,$values,$replace)
{		

		$rowc=count($rows)-1;
		$valc=count($values)-1;
		if($replace)
		{
			$sql="REPLACE into `".$table."` (";
		}
		else
		{
			$sql="INSERT into `".$table."` (";
		}
		
		
		
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."`";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.") VALUES (";
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." '". cleanforsql($values[$i])."'";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}
		$sql=$sql.")";
		mp($sql);
		mysql_query($sql) or die (mysql_error());
}

function updatesql2($table,$rows,$values, $refname, $refid)
{		

		$rowc=count($rows)-1;
		$valc=count($values)-1;


		$sql="UPDATE  `".$table."` SET ";
		
		
					for($i=0; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."` = '". cleanforsql($values[$i])."' ";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}

		$sql=$sql." WHERE `".$refname."` = '".$refid."' ";
		
		mp($sql);

		mysql_query($sql) or die (mysql_error());
}
function updatesql($table,$rows,$values, $refname, $refid)
{		

		$rowc=count($rows);
		$valc=count($values);


		$sql="UPDATE  `".$table."` SET ";
		
		
					for($i=1; $i<=$rowc; $i++)
					{
					
						$sql=$sql." `".$rows[$i]."` = '". cleanforsql($values[$i])."' ";
						if($i!=$rowc)
						{$sql=$sql.",";}
						
					}

		$sql=$sql." WHERE `".$refname."` = '".$refid."' ";
		
	//echo $sql;

		mysql_query($sql) or die (mysql_error());
}
function reverse_strrchr($haystack, $needle)
{
    $pos = strrpos($haystack, $needle);
    if($pos === false) {
        return $haystack;
    }
    return substr($haystack, 0, $pos + 1);
}

function convert_smart_quotes($string) 
{ 
    $search = array(chr(145), 
                    chr(146)); 
 
    $replace = array("'", 
                     "'"); 
 
    return str_replace($search, $replace, $string); 
}



function array_insert($array,$pos,$val)
{
    $array2 = array_splice($array,$pos);
    $array[] = $val;
    $array = array_merge($array,$array2);
   
    return $array;
}

function process_post($post,$table)
{
	$rows=array();
	$result = mysql_query("SHOW COLUMNS FROM ".$table);

	while ($row = mysql_fetch_assoc($result)) {

			$rows[]=$row['Field'];
	};
	

	foreach($post as $post_key => $value)
	{
		if (in_array($post_key, $rows)) {
		
		$rf[]=$post_key;
		$vf[]=$post[$post_key];
		}

	}
	
	$give['rows']=$rf;
	$give['values']=$vf;
	
	return $give;
	
}


function process_sql_post($post,$table,$action,$ref,$key)
{
	if($action=='add')
	{
		$var=process_post($post,$table);
		$rows=$var['rows'];
		$values=$var['values'];
		//insertsqlv2($table,$var['rows'],$var['values']);
		insert_sql($table,$var['rows'],$var['values'], TRUE);
		return true;
		mp('adding');
	}
	else if($action=='update')
	{
		$var=process_post($post,$table);
		$rows=$var['rows'];
		$values=$var['values'];
		updatesql2($table,$var['rows'],$var['values'], $ref, $key);
		//insertsqlv2($table,$var['rows'],$var['values']);
		return true;
		mp('updating');
	}
	else if($action=='replace')
	{
		$var=process_post($post,$table);
		$rows=$var['rows'];
		$values=$var['values'];
		insert_sql($table,$var['rows'],$var['values'], TRUE);
		return true;
		mp('replacing');
	}
}


function process_post_serial($post)
{
	foreach($post as $post_key => $value)
	{

		$haystack = $post_key;
		$needle = '_';
		$result = substr($haystack, 0, strpos($haystack, $needle));	

		if($result=='serial')
		{
		$y[$post_key]=$post[$post_key];	
		}
	}
	return $y;
}

function get_enum( $table , $field ){
$query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
$result = mysql_query( $query ) or die( 'error getting enum field ' . mysql_error() );
$row = mysql_fetch_array( $result , MYSQL_NUM );

$regex = "/'(.*?)'/";
preg_match_all( $regex , $row[1], $enum_array );
$enum_fields = $enum_array[1];
foreach ($enum_fields as $y)
{
	$ex[0]=$y;
	$ex[1]=$y;
	$ef[]=$ex;
	
}
rp($ef);
return( $ef );
}


function process_input($data)
{
	foreach($data as $post_key => $x)
	{
			$w[0]=$post_key;
			$w[1]=$x;
			$w[2]='=';	
			$where[]=$w;
	}
	return $where;
}

function combine_stuff($main,$addition)
{
	 	foreach($addition as $post_key => $x)
		{
			echo $x;
			$main[$post_key]=$x;
			mp($post_key.' - '.$x);
				
		}
		
		return $main;
}
?>